var express = require('express');
var router = express.Router();
var User = require('../model/model.js').User
var Note = require('../model/model.js').Note
var service = require('./service.js')

var notesPage = function (req, res) {
    var user = service.checkLogin(req, res)
    if (user) {
        Note.find({username: user.username}, function (err, docs) {
            res.render('notes', {notes: docs, user: user})
        })
    } else {
        res.redirect('/login');
    }
}


/* GET home page. */
router.get('/', function (req, res, next) {



    notesPage(req, res)
});
router.get('/test', function (req, res, next) {
    res.render('test', {title: 'foo'});
});
//
router.get('/notes', function (req, res, next) {
    if(req.session.test===undefined)
    {
        req.session.test=0;
    }else
    {
        req.session.test+=1;
    }
    console.log(req.session.test)
    notesPage(req, res)
    //res.render('test', { title: 'foo' });
});
router.get('/login', function (req, res, next) {
    var user = service.checkLogin(req, res)
    if (user) {
        res.redirect('/');
    } else {
        res.render('login', {});

    }
});
router.get('/newnote', function (req, res, next) {

    var user = service.checkLogin(req, res)
    if (user) {
        res.render('newnote', {user: user});
    } else {
        res.redirect("/", 301);
    }
});


module.exports = router;
